package com.community.common.config;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Administrator
 */
@Slf4j
@Configuration
public class InitBean {

    @Bean(name = "mySnowflake")
    public Snowflake initSnowFlake() {
        long workerId = NetUtil.ipv4ToLong(NetUtil.getLocalhostStr()) % 31;
        long dataCenterId = (long) (Math.random() * 31);
        log.info("当前机器的 workerId {} dataCenterId {}", workerId, dataCenterId);
        return IdUtil.createSnowflake(workerId, dataCenterId);
    }

}
